<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Memo extends CI_Controller {

	function tulis(){
		$data=array('form'=>'Tulis Memo');
		$this->template->set('title','.: e-Disposisi | Tulis Memo :.');
		$this->template->load('index','memo/tulis',$data);	
	}

	function inbox(){
		$data=array('form'=>'Kotak Masuk');
		$this->template->set('title','.: e-Disposisi | Inbox :.');
		$this->template->load('index','memo/inbox',$data);	
	}

   function loaduser() {
             
		$q=$this->uri->segment(3);
		 
        $sql = " select * from muser where nmuser like '%$q%' or nama_lengkap like '%$q%' ";  
		$query1 = $this->db->query($sql);  
		$coba=array();
        foreach($query1->result_array() as $resulte){ 
			$arr['query'] = $q;
			$arr['suggestions'][] = array(
				'value'	=>$resulte['nmuser'],
				'data'	=>$resulte['iduser']			
			);
			array_push($coba,$resulte['nmuser']." <".$resulte['nama_lengkap'].">");
        }
		
        echo json_encode($coba);
    	$query1->free_result();   
	}
	
	function kirim(){
		$kepada=$this->input->post('kepada');
		$judul =$this->input->post('judul');
		$isi   =$this->input->post('isi');
		$asal  =$this->session->userdata('user');		
		$tujuan=$this->split_tujuan($kepada);

		$file1=$this->input->post('file1');
		$file2=$this->input->post('file2');
		$file3=$this->input->post('file3');
		$file4=$this->input->post('file4');

		$nmfile1='';
		$nmfile2='';
		$nmfile3='';
		$nmfile4='';
	
		if(trim($file1)!=''){
			$nmfile1=$asal.date("Ymdhis").$file1;
			copy(FCPATH.'/application/upload/'.$file1, FCPATH.'/application/attachment/'.$nmfile1);
		}

		if(trim($file2)!=''){
			$nmfile2=$asal.date("Ymdhis").$file2;
			copy(FCPATH.'/application/upload/'.$file2, FCPATH.'/application/attachment/'.$nmfile2);
		}

		if(trim($file3)!=''){
			$nmfile3=$asal.date("Ymdhis").$file3;
			copy(FCPATH.'/application/upload/'.$file3, FCPATH.'/application/attachment/'.$nmfile3);
		}

		if(trim($file4)!=''){
			$nmfile4=$asal.date("Ymdhis").$file4;
			copy(FCPATH.'/application/upload/'.$file4, FCPATH.'/application/attachment/'.$nmfile4);
		}


		foreach($tujuan as $val){
			if(trim($val)!=''){	
				$waktu  = date("Y-m-d h:i:s");
				$sql	= " insert into inbox(asal,tujuan,judul,isi,file1,file2,file3,file4,waktu) 
							values('$asal','$val','$judul','$isi','$nmfile1','$nmfile2','$nmfile3','$nmfile4','$waktu') ";  
				$query1 = $this->db->query($sql);  		
			}
		}
		echo "1";
	}

	function split_tujuan($kepada){
		$hasil=array();
		$txt=explode(",",$kepada);
		foreach($txt as $value){
			$nm='';
			$value=trim($value);
			for($i=0;$i<=strlen($value);$i++){
				if(substr($value,$i,1)=='<'){
					break;			
				}else{
					$nm .=substr($value,$i,1);
				}
			}
			$hasil[]=$nm;
		}
		return $hasil; 
	}


	function do_upload($namafile){
		$config['upload_path'] = dirname($_SERVER["SCRIPT_FILENAME"]).'/application/upload/';
		$config['upload_url'] = base_url().'application/upload/';
		$config['allowed_types'] = 'jpg|png|jpeg|bmp|pdf|txt|docx|xlsx|pptx';
        $config['overwrite'] = TRUE; //overwrite user avatar
		$config['max_size']	= '2000';
		$config['file_name']  = $namafile;

		$this->load->library('upload', $config);
	    $this->upload->initialize($config);
		$this->upload->do_upload('attach1');
		$data =  $this->upload->data();
        echo json_encode($data);
	}


	function isimemo(){
		$idmemo=$this->input->post('idmemo');
        $query = $this->db->query(" select * from inbox where idmemo=$idmemo ");  
        $isi='';
		foreach($query->result_array() as $res){ 
			$isi=$res['isi'];
		}
        $query = $this->db->query(" update inbox set dibaca=1 where idmemo=$idmemo ");  

		$coba=array('isi'=>$isi);
        echo json_encode($coba);
	}

}



